import 'package:flutter/material.dart';

/// 弹窗动画类型
enum DialogAnimationType {
  slide,
  popup,
}

class MyBottomDialog {
  /// 显示自定义 Dialog
  static Future<void> show({
    required BuildContext context,
    required Widget Function(BuildContext) builder,
    required double width,
    required double height,
    double borderRadius = 16,
    bool barrierDismissible = true,
    DialogAnimationType animationType = DialogAnimationType.popup,
  }) {
    return showGeneralDialog(
      context: context,
      barrierDismissible: barrierDismissible,
      barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
      barrierColor: Colors.black54,
      transitionDuration: const Duration(milliseconds: 300),
      pageBuilder: (context, animation, secondaryAnimation) => const SizedBox.shrink(),
      transitionBuilder: (context, animation, secondaryAnimation, _) {
        final curved = CurvedAnimation(
          parent: animation,
          curve: Curves.easeOutCubic,
        );

        // Dialog UI 部分
        Widget dialog = Material(
          color: Colors.white,
          borderRadius: animationType == DialogAnimationType.slide
              ? BorderRadius.vertical(top: Radius.circular(borderRadius))
              : BorderRadius.circular(borderRadius),
          child: SizedBox(
            width: width,
            height: height,
            child: builder(context),
          ),
        );

        // 动画部分
        switch (animationType) {
          case DialogAnimationType.slide:
            return SlideTransition(
              position: Tween<Offset>(
                begin: const Offset(0, 1),
                end: Offset.zero,
              ).animate(curved),
              child: Align(
                alignment: Alignment.bottomCenter,
                child: dialog,
              ),
            );

          case DialogAnimationType.popup:
          default:
            return ScaleTransition(
              scale: curved,
              child: Align(
                alignment: Alignment.center,
                child: dialog,
              ),
            );
        }
      },
    );
  }
}
